package com.patni.mainface;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ResizeFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean isTopLeft;
	private boolean isTop;
	private boolean isTopRight;
	private boolean isRight;
	private boolean isBottomRight;
	private boolean isBottom;
	private boolean isBottomLeft;
	private boolean isLeft;
	private boolean isDraggeAble;
	private int tempx;
	private int tempy;
	private int MIN_WIDTH = 100;
	private int MIN_HEIGHT = 100;
	private int HEAD_IMAGE_HEIGHT;
	private final static int RESIZE_WIDTH = 2;
	private final static int HEAD_HEIGHT = 50;
	
	private String headImageref;
	private static BufferedImage headImage;
	private static Icon headImage1;
	
	public ResizeFrame() {
		this.setUndecorated(true);
		this.addMouseMotionListener(new ResizeAdapter(this));
	}

	public void SetMinSize(int width, int height) {
		MIN_WIDTH = width;
		MIN_HEIGHT = height;
	}
	
//	public void setHeadHeight(){
//		HEAD_IMAGE_HEIGHT=height;
//	}
	
	public void setHeadImage(String ref,int height){
		HEAD_IMAGE_HEIGHT=height;
		this.headImageref=ref;
		
		if(headImage==null){
			try { 
				
			headImage = ImageIO.read(new File(headImageref)); 
			} catch (IOException e) { 
			e.printStackTrace(); 
			} 
			}
		
		if(headImage1==null){
			headImage1=new ImageIcon(ref);
		}
	}
	
	public void paint(Graphics g) { 
		super.paint(g);
 
	
		
		
		g.drawImage(headImage,0,0,this.getWidth(),HEAD_IMAGE_HEIGHT,this); 
//		g.dra
		
		} 
	
	private class ResizeAdapter extends MouseAdapter {
		private Component c;

		public ResizeAdapter(Component c) {
			this.c = c;
		}

		@Override
		public void mouseMoved(MouseEvent event) {
			int x = event.getX();
			int y = event.getY();
			int width = c.getWidth();
			int height = c.getHeight();

//			System.out.println("Moved: X:" + x + " Y:" + y + " Width:" + width
//					+ " Height:" + height + " isD:" + isDraggeAble);

			int cursorType = Cursor.DEFAULT_CURSOR;
			isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = 
				isBottomLeft = isLeft = isDraggeAble = false;
			if (y <= RESIZE_WIDTH) {
				if (x <= RESIZE_WIDTH) {
					isTopLeft = true;
					cursorType = Cursor.NW_RESIZE_CURSOR;
				} else if (x >= width - RESIZE_WIDTH) {
					isTopRight = true;
					cursorType = Cursor.NE_RESIZE_CURSOR;
				} else {
					isTop = true;
					cursorType = Cursor.N_RESIZE_CURSOR;
				}
			} else if (y >= height - RESIZE_WIDTH) {
				if (x <= RESIZE_WIDTH) {
					isBottomLeft = true;
					cursorType = Cursor.SW_RESIZE_CURSOR;
				} else if (x >= width - RESIZE_WIDTH) {
					isBottomRight = true;
					cursorType = Cursor.SE_RESIZE_CURSOR;
				} else {
					isBottom = true;
					cursorType = Cursor.S_RESIZE_CURSOR;
				}
			} else if (x <= RESIZE_WIDTH) {
				isLeft = true;
				cursorType = Cursor.W_RESIZE_CURSOR;
			} else if (x >= width - RESIZE_WIDTH) {
				isRight = true;
				cursorType = Cursor.E_RESIZE_CURSOR;
			} else if (!isTopLeft && !isTop && !isTopRight && !isRight
					&& !isBottomRight && !isBottom && !isBottomLeft && !isLeft) {
				if (y < HEAD_HEIGHT)
					isDraggeAble = true;
				else
					isDraggeAble = false;

				tempx = x;
				tempy = y;
			}
			c.setCursor(new Cursor(cursorType));
		}

		@Override
		public void mouseDragged(MouseEvent event) {
			int x = event.getX();
			int y = event.getY();
			int width = c.getWidth();
			int height = c.getHeight();

//			System.out.println("Dragged: X:" + x + " Y:" + y + " Width:"
//					+ width + " Height:" + height);
			if (width >= MIN_WIDTH && height >= MIN_HEIGHT) {
				if (isTopLeft) {
					c.setLocation(c.getX() + x, c.getY() + y);
					c.setSize(width - x, height - y);
				}
				else if (isTop) {
					c.setLocation(c.getX(), c.getY() + y);
					c.setSize(width, height - y);
				}
				else if (isTopRight) {
					c.setLocation(c.getX(), c.getY() + y);
					c.setSize(x, height - y);
				}
				else if (isRight) {
					c.setSize(x, height);
				}
				else if (isBottomRight) {
					c.setSize(x, y);
				}
				else if (isBottom) {
					c.setSize(width, y);
				}
				else if (isBottomLeft) {
					c.setLocation(c.getX() + x, c.getY());
					c.setSize(width - x, y);
				}
				else if (isLeft) {
					c.setLocation(c.getX() + x, c.getY());
					c.setSize(width - x, height);
				}
				else if (isDraggeAble) {
					Point mousePoint = MouseInfo.getPointerInfo().getLocation();
					int mx = mousePoint.x;
					int my = mousePoint.y;
					c.setLocation(mx - tempx, my - tempy);
					c.repaint();
				}
			} else {
				int cursorType = Cursor.DEFAULT_CURSOR;
				isTopLeft = isTop = isTopRight = isRight = isBottomRight = 
					isBottom = isBottomLeft = isLeft = false;
				c.setCursor(new Cursor(cursorType));

				if (width < MIN_WIDTH) {
					width = MIN_WIDTH;
				}

				if (height < MIN_HEIGHT) {
					height = MIN_HEIGHT;
				}

				c.setSize(width, height);
			}
		}
	}


}
